﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace Proxy3
{
    class Utility
    {
        WebBrowser webBrowser1 = new WebBrowser();

        string extraInfo;


        /// <summary>
        /// 从官网上获取升级信息
        /// </summary>
        public String GetUpdateInfo()
        {
            var request = (HttpWebRequest)HttpWebRequest.Create("http://chong.b3log.org/software/proxy.txt");

            request.Accept = "*/*";
            request.UserAgent = "Mozilla/4.0 (MSIE 6.0; Windows NT 8982 )";
            request.Headers["Accept-Language"] = "zh-cn";
            request.Timeout = 6000;

            try
            {
                HttpWebResponse oWebResp = (HttpWebResponse)request.GetResponse();
                StreamReader oStream = new StreamReader(oWebResp.GetResponseStream(), System.Text.Encoding.GetEncoding("UNICODE"));
                string sResp = oStream.ReadToEnd();
                webBrowser1.DocumentText = sResp;
                oStream.Close();
                MessageBox.Show(sResp, "升级信息");
                return sResp;
            }
            catch
            {
                MessageBox.Show("貌似网络错误……");
                return "error";
            }
        }


        public string[] GetProxyInfo()
        {

            Application.UseWaitCursor = true;

            Stopwatch watch = new Stopwatch();
            watch.Start();
            // 访问主页
            webBrowser1.Navigate("http://chong.b3log.org");
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
                if (watch.ElapsedMilliseconds > 3000)
                    break;
            }


            // 访问代理页面

            webBrowser1.Navigate("http://chong.b3log.org/software/proxy.htm");
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
                if (watch.ElapsedMilliseconds > 100000)
                    break;
            }


            // Get Extra Info
            var list = webBrowser1.Document.GetElementById("info");
            if (list == null)
            {
                extraInfo = "error";
            }
            else
            {
                extraInfo = list.InnerHtml;
            }

            // Get Proxy List
            list = webBrowser1.Document.GetElementById("proxylist");
            if (list == null)
            {
                MessageBox.Show("网络错误");
                Application.UseWaitCursor = false;
                return null;
            }
            var proxys = list.InnerHtml.Split(new string[] { ",", "\n","\t"," " }, StringSplitOptions.RemoveEmptyEntries);

            


            for (int i = 0; i < proxys.Length; i++)
                proxys[i] = proxys[i].Trim(new char[]{' ','\t',''});

            Application.UseWaitCursor = false;
            return proxys;
        }


        /// <summary>
        /// 测试当前代理是否可用
        /// </summary>
        /// <returns></returns>
        public int TestCurrentProxy(bool bMsg = false)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            var request = (HttpWebRequest)HttpWebRequest.Create("http://chong.b3log.org/");

            request.Accept = "*/*";
            request.UserAgent = "Mozilla/4.0 (MSIE 6.0; Windows NT 8982 )";
            request.Headers["Accept-Language"] = "zh-cn";
            request.Timeout = 12000;
            try
            {
                webBrowser1.Navigate("www.solesschong.tk");

                HttpWebResponse oWebResp = (HttpWebResponse)request.GetResponse();
                StreamReader oStream = new StreamReader(oWebResp.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                string sResp = oStream.ReadToEnd();
                webBrowser1.DocumentText = sResp;
                oStream.Close();
            }
            catch
            {
                return -1;
            }
            watch.Stop();
            return (int)(1000000F / watch.ElapsedMilliseconds);
        }
    }
}
